class HashTable {
  constructor () {
    this.table = new Array(137)
  }

  simpleHash (data) {
    let total = 0
    for (let i = 0; i < data.length; i++) {
      total += data.charCodeAt(i)
    }
    return total % this.table.length
  }

  betterHash (string) {
    const H = 39
    var total = 0
    for (var i = 0; i < string.length; ++i) {
      total += H * total + string.charCodeAt(i)
    }
    total = total % this.table.length
    if (total < 0) {
      total += this.table.length - 1
    }
    console.log('Hash value: ' + string + ' -> ' + total)
    return parseInt(total)
  }

  put (key, value) {
    let pos = this.betterHash(key)
    this.table[pos] = value
  }

  get (key) {
    return this.table[this.betterHash(key)]
  }

  showDistro () {
    var n = 0
    for (var i = 0; i < this.table.length; ++i) {
      if (this.table[i] != undefined) {
        console.log(i + ': ' + this.table[i])
      }
    }
  }
}


